import shop from '../../api/shop'

const state={
    all:[]
}

const getters={}

const actions={
    getAllProducts({commit}){//获取所有产品列表
        shop.getProducts(products=>{
            commit('setProducts',products)
        })
    }
}

const mutations={
    setProducts(state,products){//将产品列表赋值给all
        state.all=products
    },
    decrementProductInventory(state,{id}){//减少数量
        const product=state.all.find(product=>product.id===id)
        product.inventory--
    }
}

export default{
    namespaced: true,
    state,
    getters,
    actions,
    mutations
}